package myself.study.network.tcp.examples.ex02;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
* @Name: TcpServer
* @Description: 案例二：将客户端键盘输入的数据，写入到文件中
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-1-12 下午9:06:17 
* @Version: V1.0
 */
public class TcpServer {
	
	public static void main(String[] args) throws IOException {
		//创建服务端Socket对象
		ServerSocket server = new ServerSocket(12345) ;
		//监听客户端连接
		Socket socket = server.accept() ;
		//获取输入流对象，并包装成高效缓冲读取字符流
		BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())) ;
		//创建文件输入流对象，并将其封装成高效缓冲写入字符流
		BufferedWriter writer = new BufferedWriter(new FileWriter("keyword.txt")) ;
		String line = null ;
		while((line = reader.readLine()) != null) {
			//System.out.println(line);
			writer.write(line) ;
			writer.newLine() ;
			writer.flush() ;
		}
		writer.close() ;
		socket.close() ;
	}

}






